﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using PetaPoco;

namespace DAL.MySQL
{
    public class adDAL
    {
        public Database db = DbScoure.DataBase();
        public adEntity GetOne(int id)
        {
            return db.SingleOrDefault<adEntity>("select * from ad where id=@0", id);
        }
        public List<admodel> GetList(string where, string order, params object[] args)
        {
            var list = db.Fetch<admodel>("select a.*,b.title as classname,b.tag,b.show from ad as a left join adclass as b on a.classid=b.ID where 1=1 " + where + " order by " + order + " ,a.ID desc", args);
            return list;
        }

        public Page<admodel> GetPageList(int pagenum, int pagesize, string where, string order, params object[] args)
        {
            var pagelist = db.Page<admodel>(pagenum, pagesize, "select a.*,b.title as classname,b.tag,b.show from ad as a left join adclass as b on a.classid=b.ID where 1=1 " + where + " order by " + order + " ,a.ID desc", args);
            return pagelist;
        }

        public int Update(object obj,int ID)
        {
             return db.Update("ad", "ID", obj, ID);
        }
        public adEntity Save(adEntity model)
        {
            db.Save("ad", "ID", model);
            return model;
        }

        public bool Del(int id)
        {
            try
            {
                db.Delete("ad", "ID", null, id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Del(adEntity model)
        {
            try
            {
                db.Delete("ad", "ID", model);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
